package com.gitee.wsl.doc.prosemirror.model.node.ext

import com.gitee.wsl.doc.prosemirror.exception.ReplaceError
import com.gitee.wsl.doc.prosemirror.model.node.Node
import com.gitee.wsl.doc.prosemirror.model.node.inline.TextNode

fun checkJoin(main: Node, sub: Node) {
    if (!sub.type.compatibleContent(main.type)) {
        throw ReplaceError("Cannot join ${sub.type.name} onto ${main.type.name}")
    }
}

fun addNode(child: Node, target: MutableList<Node>) {
    val last = target.size - 1
    if (last >= 0 && child.isText && child.sameMarkup(target[last])) {
        target[last] = (child as TextNode).withText(target[last].text + child.text)
    } else {
        target.add(child)
    }
}

